package utils

import (
	"gitcode.com/bangxu/go-wg/configs"
	"io"
	"log"
	"os"
)

// Logger 是你的日志类类型
type Logger struct {
	*log.Logger
}

var (
	ErrorLog *Logger
	InfoLog  *Logger
)

func init() {
	configs.LoadConfig("./etc/conf/gowg.json")
	ErrorLog = NewLogger(configs.AppConfig.LogFilePaths.ErrorLogPath, "ERROR: ")
	InfoLog = NewLogger(configs.AppConfig.LogFilePaths.InfoLogPath, "INFO: ")
}

func NewLogger(logFile string, prefix string) *Logger {
	file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalf("failed to open log file: %s", err)
	}

	// 创建同时写入文件和控制台的写入器
	multiWriter := io.MultiWriter(file, os.Stdout)

	logger := log.New(multiWriter, prefix, log.Ldate|log.Ltime|log.Lshortfile)
	return &Logger{logger}
}
